#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long
using namespace std;
const int N=1e7+5,P=998244353;
int n,m,A,B,C,X;int cnt[N],pre[N],nxt[N];ldb f[N];pair<int,int>g[N];
inline int qpow(int x,int y,int z=1){
	for(;y;(y>>=1)&&(x=(LL)x*x%P))if(y&1)z=(LL)z*x%P;return z;
}
inline bool check(const ldb&p){
	f[1]=min(p*2,cnt[1]>1?1-p:1.L);
	for(int i=2;i<=n;++i){
		f[i]=min(f[i-1]+1-3*p,1-p);
		if(cnt[i]<2)f[i]+=p;
		if(f[i]<0)return 0;
	}
	return 1;
}
inline int solve(const ldb&p){
	if((p*2)<=(cnt[1]>1?1-p:1.L))f[1]=p*2,g[1]=make_pair(2,0);
	else f[1]=(cnt[1]>1?1-p:1.L),g[1]=make_pair(cnt[1]>1?-1:0,1);
	for(int i=2;i<=n;++i){
		if((f[i-1]+1-3*p)<=(1-p))f[i]=f[i-1]+1-3*p,g[i]=make_pair(g[i-1].first-3,g[i-1].second+1);
		else f[i]=1-p,g[i]=make_pair(-1,1);
		if(cnt[i]<2)f[i]+=p,++g[i].first;
	}
	const int q=min_element(f+1,f+n+1)-f;
	return qpow(g[q].first,P-2,-g[q].second);
}
inline void MAIN(){
	cin>>n>>X>>A>>B>>C>>m,fill(cnt+1,cnt+m+1,0);
	for(int i=1;i<=n;++i)X=((1ll*A*X+B)%m*X+C)%m+1,++cnt[X];
	n=0;
	for(int i=1;i<=m;++i)if(cnt[i])cnt[++n]=cnt[i];
	ldb l=0,r=1;
	for(int t=40;t--;){
		const ldb mid=(l+r)/2;
		if(check(mid))l=mid;else r=mid;
	}
	const int ans=solve(l);
	cout<<l<<" "<<(ans<0?ans+P:ans)<<'\n';
}
signed main(){
    cin.tie(0)->sync_with_stdio(0);
	freopen("guess.in","r",stdin);
	freopen("guess.out","w",stdout);
	int t=1;cin>>t;while(t--)MAIN();
    return 0;
}
/*
x*y
*/